﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.CommonDao;
using CSP.Dao.IObjectDaos.CSP;
using CSP.DataAccess;
using CSP.Model.CSP;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class SearchCacheDao : CSPCommonDao, ISearchCacheDao
    {
        public SearchCacheDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }


        public ICollection<Model.CSP.SearchCache> FindAll()
        {
            return _context.SearchCaches.ToArray();
        }

        public Model.CSP.SearchCache GetDataById(int id)
        {
            return _context.SearchCaches.FirstOrDefault(a => a.id == id);
        }

        public ICollection<Model.CSP.SearchCache> GeDatatByIds(int[] Ids)
        {
            return _context.SearchCaches.Where(a => Ids.Contains(a.id)).ToArray();
        }

        public void Add(Model.CSP.SearchCache obj)
        {
            _context.SearchCaches.Add(obj);
            _context.SaveChanges();
        }

        public void Update(Model.CSP.SearchCache obj)
        {
            SearchCache searchCache = GetDataById(obj.id);
            if (searchCache != null)
            {
                searchCache = obj;
            }
            _context.SaveChanges();
        }

        public SearchCache DeleteById(int id)
        {
            SearchCache searchCache = GetDataById(id);
            _context.SearchCaches.Remove(searchCache);
            _context.SaveChanges();

            return searchCache;
        }

        public SearchCache Remove(SearchCache searchCache)
        {
            _context.SearchCaches.Remove(searchCache);
            _context.SaveChanges();

            return searchCache;
        }

        public List<SearchCache> FindSearchCacheBySearchInfo(Nullable<int> serviceTypeId, int? serviceArea,string ageGroupType, string SearchValue)
        {

            List<SearchCache> result = new List<SearchCache>();
            IQueryable<SearchCache> searchCaches;

            if (serviceTypeId == null && serviceArea == null && ageGroupType == null)
            {
                searchCaches = _context.SearchCaches.Where(s => (s.ServiceType == null && s.ServiceArea == null && s.AgeGroup == null && s.SearchValue == SearchValue));
            }
            else if (serviceTypeId == null && serviceArea == null && ageGroupType != null)
            {
                searchCaches = _context.SearchCaches.Where(s => (s.ServiceType == serviceTypeId && s.ServiceArea == serviceArea && s.SearchValue == SearchValue)
                   || (s.ServiceType == null && s.ServiceArea == null && s.SearchValue == "")
                   );
            }
            else if (serviceTypeId == null && serviceArea != null && ageGroupType != null)
            {
                searchCaches = _context.SearchCaches.Where(s => (s.ServiceType == null && s.ServiceArea == serviceArea && s.SearchValue == SearchValue)
               || (s.ServiceType == null && s.ServiceArea == serviceArea && s.SearchValue == "")
               || (s.ServiceType == null && s.ServiceArea == serviceArea && s.AgeGroup == null && s.SearchValue == "")
               );
            }
            else if (serviceTypeId != null && serviceArea == null && ageGroupType == null)
            {
                searchCaches = _context.SearchCaches.Where(s => (s.ServiceType == serviceTypeId && s.ServiceArea == null && s.AgeGroup == null && s.SearchValue == SearchValue)
               || (s.ServiceType == serviceTypeId && s.ServiceArea == null && s.AgeGroup == null && s.SearchValue == "")
               );
            }
            else if (serviceTypeId != null && serviceArea != null && ageGroupType == null)
            {
                searchCaches = _context.SearchCaches.Where(s => (s.ServiceType == serviceTypeId && s.ServiceArea == serviceArea && s.AgeGroup == null && s.SearchValue == SearchValue)
                       || (s.ServiceType == serviceTypeId && s.ServiceArea == serviceArea && s.AgeGroup == null && s.SearchValue == "")
                       || (s.ServiceType == serviceTypeId && s.ServiceArea == null && s.AgeGroup == null && s.SearchValue == "")
                       );
            }
            else if (serviceTypeId != null && serviceArea == null && ageGroupType != null)
            {
                searchCaches = _context.SearchCaches.Where(s => (s.ServiceType == serviceTypeId && s.ServiceArea == null && s.SearchValue == SearchValue)
               || (s.ServiceType == serviceTypeId && s.ServiceArea == null && s.SearchValue == "")
               || (s.ServiceType == serviceTypeId && s.ServiceArea == null && s.AgeGroup == null && s.SearchValue == "")
               );
            }
            else
            {

                searchCaches = _context.SearchCaches.Where(s => (s.ServiceType == serviceTypeId && s.ServiceArea == serviceArea && s.SearchValue == SearchValue)
                    || (s.ServiceType == serviceTypeId && s.ServiceArea == serviceArea && s.SearchValue == "")
                    || (s.ServiceType == serviceTypeId && s.ServiceArea == serviceArea && s.AgeGroup == null && s.SearchValue == "")
                    || (s.ServiceType == serviceTypeId && s.ServiceArea == null && s.AgeGroup == null && s.SearchValue == "")
                    );
            }

            if (searchCaches.Count() != 0)
            {
                result = searchCaches.ToList();
            }

            return result;
        }
    }
}
